﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace hyjiacan.util.winform
{
    public class JForm : Form
    {
        protected internal Label title;
        protected internal JSmallButton bClose;
        protected internal JSmallButton bMin;
        protected internal JSmallButton bMax;
        protected internal Panel workBench;
        private ToolTip toolTip;
        private System.ComponentModel.IContainer components;
        private System.ComponentModel.BackgroundWorker iconLoader;
        private Label separator;
        protected internal PictureBox favicon;

        /// <summary>
        /// 设置窗口的图标
        /// </summary>
        /// <param name="image"></param>
        public void SetIcon(Image image)
        {
            this.favicon.Image = image;
        }
        /// <summary>
        /// 设置窗口的图标
        /// </summary>
        /// <param name="stream"></param>
        public void SetIcon(System.IO.Stream stream)
        {
            SetIcon(new Bitmap(stream));
        }
        /// <summary>
        /// 设置窗口的图标
        /// </summary>
        /// <param name="url">网络图片地址</param>
        public void SetIcon(string url)
        {
            iconLoader.RunWorkerAsync(url);
        }

        public JForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(JForm));
            this.workBench = new System.Windows.Forms.Panel();
            this.title = new System.Windows.Forms.Label();
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            this.iconLoader = new System.ComponentModel.BackgroundWorker();
            this.separator = new System.Windows.Forms.Label();
            this.favicon = new System.Windows.Forms.PictureBox();
            this.bClose = new hyjiacan.util.winform.JSmallButton();
            this.bMin = new hyjiacan.util.winform.JSmallButton();
            this.bMax = new hyjiacan.util.winform.JSmallButton();
            ((System.ComponentModel.ISupportInitialize)(this.favicon)).BeginInit();
            this.SuspendLayout();
            // 
            // workBench
            // 
            this.workBench.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.workBench.Location = new System.Drawing.Point(4, 51);
            this.workBench.Name = "workBench";
            this.workBench.Size = new System.Drawing.Size(792, 428);
            this.workBench.TabIndex = 17;
            // 
            // title
            // 
            this.title.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.title.AutoEllipsis = true;
            this.title.Font = new System.Drawing.Font("Microsoft YaHei UI", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.title.Location = new System.Drawing.Point(42, 9);
            this.title.Name = "title";
            this.title.Size = new System.Drawing.Size(651, 32);
            this.title.TabIndex = 1;
            this.title.Text = "站点名称";
            this.title.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.title.TextChanged += new System.EventHandler(this.title_TextChanged);
            this.title.MouseDown += new System.Windows.Forms.MouseEventHandler(this.JForm_MouseDown);
            // 
            // iconLoader
            // 
            this.iconLoader.DoWork += new System.ComponentModel.DoWorkEventHandler(this.iconLoader_DoWork);
            // 
            // separator
            // 
            this.separator.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.separator.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.separator.Location = new System.Drawing.Point(0, 47);
            this.separator.Name = "separator";
            this.separator.Size = new System.Drawing.Size(800, 1);
            this.separator.TabIndex = 19;
            this.separator.Text = "label1";
            // 
            // favicon
            // 
            this.favicon.Image = global::hyjiacan.util.winform.Properties.Resources._60x60;
            this.favicon.InitialImage = null;
            this.favicon.Location = new System.Drawing.Point(4, 9);
            this.favicon.Name = "favicon";
            this.favicon.Size = new System.Drawing.Size(32, 32);
            this.favicon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.favicon.TabIndex = 0;
            this.favicon.TabStop = false;
            this.favicon.MouseDown += new System.Windows.Forms.MouseEventHandler(this.JForm_MouseDown);
            // 
            // bClose
            // 
            this.bClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.bClose.BackColor = System.Drawing.Color.Red;
            this.bClose.FlatAppearance.BorderColor = System.Drawing.Color.Red;
            this.bClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.bClose.Font = new System.Drawing.Font("Microsoft YaHei", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.bClose.ForeColor = System.Drawing.Color.White;
            this.bClose.Location = new System.Drawing.Point(764, 9);
            this.bClose.Name = "bClose";
            this.bClose.Size = new System.Drawing.Size(32, 32);
            this.bClose.TabIndex = 15;
            this.bClose.Text = "关";
            this.toolTip.SetToolTip(this.bClose, "关闭");
            this.bClose.UseVisualStyleBackColor = false;
            this.bClose.Click += new System.EventHandler(this.bClose_Click);
            // 
            // bMin
            // 
            this.bMin.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.bMin.BackColor = System.Drawing.Color.Purple;
            this.bMin.Enabled = false;
            this.bMin.FlatAppearance.BorderColor = System.Drawing.Color.Black;
            this.bMin.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.bMin.Font = new System.Drawing.Font("Microsoft YaHei", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.bMin.ForeColor = System.Drawing.Color.White;
            this.bMin.Location = new System.Drawing.Point(698, 9);
            this.bMin.Name = "bMin";
            this.bMin.Size = new System.Drawing.Size(32, 32);
            this.bMin.TabIndex = 16;
            this.bMin.Text = "小";
            this.toolTip.SetToolTip(this.bMin, "最小化");
            this.bMin.UseVisualStyleBackColor = false;
            this.bMin.Click += new System.EventHandler(this.bMin_Click);
            // 
            // bMax
            // 
            this.bMax.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.bMax.BackColor = System.Drawing.Color.Blue;
            this.bMax.Enabled = false;
            this.bMax.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
            this.bMax.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.bMax.Font = new System.Drawing.Font("Microsoft YaHei", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.bMax.ForeColor = System.Drawing.Color.White;
            this.bMax.Location = new System.Drawing.Point(731, 9);
            this.bMax.Name = "bMax";
            this.bMax.Size = new System.Drawing.Size(32, 32);
            this.bMax.TabIndex = 14;
            this.bMax.Text = "大";
            this.toolTip.SetToolTip(this.bMax, "最大化");
            this.bMax.UseVisualStyleBackColor = false;
            this.bMax.Click += new System.EventHandler(this.bMax_Click);
            // 
            // JForm
            // 
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(800, 480);
            this.Controls.Add(this.separator);
            this.Controls.Add(this.workBench);
            this.Controls.Add(this.title);
            this.Controls.Add(this.bClose);
            this.Controls.Add(this.favicon);
            this.Controls.Add(this.bMin);
            this.Controls.Add(this.bMax);
            this.Font = new System.Drawing.Font("Microsoft YaHei UI", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "JForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.JForm_MouseDown);
            ((System.ComponentModel.ISupportInitialize)(this.favicon)).EndInit();
            this.ResumeLayout(false);

        }

        private void bClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void bMax_Click(object sender, EventArgs e)
        {
            JSmallButton b = ((JSmallButton)sender);
            if (this.WindowState == FormWindowState.Maximized)
            {
                b.Text = "大";
                toolTip.SetToolTip(b, "最大化");
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                b.Text = "普";
                toolTip.SetToolTip(b, "普通大小");
                this.WindowState = FormWindowState.Maximized;
            }
        }

        private void bMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        /// <summary>
        /// 加载网络图片的后台线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void iconLoader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            try
            {
                SetIcon(System.Net.WebRequest.Create(e.Argument.ToString()).GetResponse().GetResponseStream());
            }
            catch { }
        }

        private void JForm_MouseDown(object sender, MouseEventArgs e)
        {
            NativeMethods.ReleaseCapture();
            NativeMethods.SendMessage(this.Handle, 0x0112, 0xF012, 0);
        }

        private void title_TextChanged(object sender, EventArgs e)
        {
            this.Text = this.title.Text;
        }
    }
}
